package com.jr.controller;

import com.jr.entity.*;
import com.jr.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;


@RestController
public class UserController {
    @Autowired
    private Basic_infoService basic_infoService;
    @Autowired
    private Job_intentionService job_intentionService;
    @Autowired
    private Max_xueliService max_xueliService;
    @Autowired
    private CorrelationService correlationService;
    @Autowired
    private UserService userService;

    @RequestMapping("/log")
    public ModelAndView lo() {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("login.html");
        return modelAndView;

    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping("/register")
    public ModelAndView register() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("register");
        return mv;
    }

    @RequestMapping("/adduser")
    public boolean adduser(String phoneoremail, String password) {

        User user = new User();
        if (phoneoremail.length() > 0) {
            if (phoneoremail.contains("@")) {
                User finduseremail = userService.finduseremail(phoneoremail);
                if (finduseremail == null) {
                    user.setEmail(phoneoremail);
                    user.setPassword(password);
                    userService.insertUser(user);
                    return true;
                } else {
                    return false;
                }
            } else {
                User finduserphone = userService.finduserphone(phoneoremail);
                if (finduserphone == null) {
                    user.setPhone(phoneoremail);
                    user.setPassword(password);
                    userService.insertUser(user);
                    return true;
                } else {
                    return false;
                }

            }
        } else {
            return false;
        }

    }

    @RequestMapping("/register1")
    public ModelAndView register1() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("login");
        return mv;
    }

    @RequestMapping("/login")
    public String login(String phoneoremail, String password) {

        User user = new User();
        StringBuilder str = new StringBuilder("{");
        if (phoneoremail.length() > 0) {
            if (phoneoremail.contains("@")) {
                User finduseremail = userService.finduseremail(phoneoremail);
                if (finduseremail != null) {
                    user.setEmail(phoneoremail);
                    user.setPassword(password);
                    ArrayList<User> users = userService.loginUser(user);


                    if (users.size() > 0) {

                        Integer uid=users.get(0).getUid();
                        str.append("\"info\":\"登录成功\",");
                        str.append("\"uid\":"+uid+"}");


                        return str.toString();
                    } else {
                        str.append("\"info\":\"登录失败\"}");
                        return str.toString();
                    }
                } else {
                    str.append("\"info\":\"登录失败\"}");
                    return str.toString();
                }
            } else {
                User finduserphone = userService.finduserphone(phoneoremail);
                if (finduserphone != null) {
                    user.setPhone(phoneoremail);
                    user.setPassword(password);
                    ArrayList<User> users = userService.loginUser(user);

                    if (users.size() > 0) {
                        Integer uid = users.get(0).getUid();
                        str.append("\"info\":\"登录成功\",");
                        str.append("\"uid\":" + uid + "}");

                        return str.toString();
                    } else {
                        str.append("\"info\":\"登录失败\"}");
                        return str.toString();
                    }
                } else {
                    str.append("\"info\":\"登录失败\"}");
                    return str.toString();
                }
            }

        } else {
            str.append("\"info\":\"登录失败\"}");
            return str.toString();
        }
    }


    @RequestMapping("/login1")
    public ModelAndView login1(String uid) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("uid", uid);
        mv.setViewName("index");
        return mv;


    }

    @RequestMapping("/password")
    public ModelAndView password() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("password");
        return mv;
    }

    @RequestMapping("/pwd")
    public boolean pwd(String account, String password1, String password2) {


        if (!password1.equals(password2)) {
            return false;
        } else {
            if (account.contains("@")) {
                User user = userService.finduseremail(account);
                if (user != null) {
                    user.setPassword(password2);
                    userService.update2(user);
                    return true;
                } else {
                    return false;
                }
            } else {
                User user = userService.finduserphone(account);
                if (user != null) {
                    user.setPassword(password2);
                    userService.update2(user);
                    return true;
                } else {
                    return false;
                }
            }
        }
    }

    /**
     * 跳转个人中心页面
     */
    @RequestMapping("/personal_resume")

    public ModelAndView personal_resume() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("personal_resume");
        return mv;
    }

    /**
     * 新增基本信息,工作意向
     */
    @RequestMapping("/addall")
    public ModelAndView add(Basic_info basic_info, Job_intention job_intention, Max_xueli max_xueli) {
        ModelAndView mv = new ModelAndView();
        basic_infoService.insert(basic_info);
        job_intentionService.insert(job_intention);
        max_xueliService.insert(max_xueli);

        //通过basic的属性找到bid

        Correlation correlation = new Correlation();
        correlation.setBid(basic_infoService.selectByName(basic_info.getName()).get(0).getBid());
        correlation.setJid(job_intentionService.selectByStatus(job_intention.getStatus()).get(0).getJid());
        correlation.setMid(max_xueliService.selectBySchool(max_xueli.getSchool()).get(0).getMid());
        correlationService.insert(correlation);
        mv.setViewName("login");
        return mv;

    }

    @RequestMapping("/ps")
    private ModelAndView myjob(String uid) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("personal_selected");
        modelAndView.addObject("uid", uid);
        return modelAndView;
    }

}
